﻿
var $zen;

dojo.declare("zen.zenManager", null,
    {
        constructor: function (themeClass) {

            console.log('zen.Callback created');

            document.body.className = themeClass;

            this.maskDialog = new dijit.Dialog({ style: "width:240px;height:36px", closeable: false, draggable: false, doLayout: false, baseClass: "zenMaskDialog" }, "zenMaskDialog");
            this.maskDialog.titleBar.style.display = 'none';
            //this.maskDialog.startup();

            dojo.addOnLoad(function () {
                dojo.style("zenMaskPage", "display", "none");
                $zen.hideMask();
            });

            if (document.forms.length > 0 && dojo.exists("dijit.form.Form")) {
                this.form = new dijit.form.Form({ method: "POST" }, document.forms[0]);

                dojo.connect(this.form, "onSubmit", function (e) {

                    var result = this.validate();

                    if (result)
                        $zen.showMask();

                    return result;
                    //alert("Ready to submit data: " + dojo.toJson($zen$form.attr("value")));
                });

                this.form.startup();

                console.log('zen.From handler attached');
            }



        },
        callbackSuccess: function (data, ioArgs) {

            if (data.success) {
                alert("Callback success!");
                alert("Html Payload:" + data.htmlPayload);
                alert("Script Payload:" + data.scriptPayload);
                alert("Message:" + data.message);
                //alert(dojo.body);

                if (data.htmlPayload.length > 0) {
                    dojo.place(data.htmlPayload, "zenCallbackContent", "after");
                }

                if (data.scriptPayload.length > 0) {
                    dojo.eval(data.scriptPayload);
                }


                dojo.eval(data.scriptLayout);
            }
            else {

                //todo: check if dialog is loaded?
                var exceptionDialog = new dijit.Dialog(
                            {
                                style: "width:500px;height:300px",
                                title: "Request Failed",
                                autofocus: true,
                                draggable: true,
                                hasShadow: true,
                                content: data.message
                            }, "zenCallbackDialog");

                exceptionDialog.show();

            }
        },
        callbackFailure: function (data, ioArgs) {
            alert('Callback failure: ' + data);
        },
        execute: function (targetUrl, methodName) {

            dojo.xhrGet({
                url: targetUrl,
                load: this.callbackSuccess,
                error: this.callbackFailure,
                handleAs: "json",
                content: { zenIsCallback: true, zenMethod: methodName }
            });
        },
        showMask: function () {
            this.maskDialog.show();
        },
        hideMask: function () {
            this.maskDialog.hide();
        }
    }
);

